/*
 *  Copyright 2009 David Kuhn <david.s.kuhn at gmail.com>.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.google.code.teamtools.client.data;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.data.DataReader;
import java.lang.Object;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author David Kuhn <david.s.kuhn at gmail.com>
 */
public class TreeBeanModelReader  implements DataReader<List<BeanModel>>
{
    public List<BeanModel> read(Object loadConfig, Object data)
    {
        if (data instanceof List) {
            List<BeanModel> result = new ArrayList<BeanModel>();
            List<Object> beans = (List) data;
            for (Object bean : beans) {
                BeanModelFactory factory = BeanModelLookup.get().getFactory(
                        bean.getClass());

                if (factory == null) {
                    throw new RuntimeException("No BeanModelFactory found for "
                            + bean.getClass());
                } else {
                    result.add(factory.createModel(bean));
                }
            }
            return result;
        } else {
            throw new RuntimeException("Error converting data");
        }
    }
}
